package ru.spbau.database.group3.types;

import java.io.Serializable;
import java.nio.ByteBuffer;

/**
 * @author: stas
 * @date: 2/4/13
 * @time: 2:37 PM
 */
public class DoubleType implements Type, Serializable {


    public DoubleType() {
    }

    @Override
    public Object valueOf(String s) {
        return Double.valueOf(s);
    }


    @Override
    public int size() {
        return Double.SIZE / 8;
    }

    @Override
    public void writeValueToByteBuffer(ByteBuffer data, Object keyValue) {
        data.putDouble((Double) keyValue);
    }


    @Override
    public Object readValueFromByteBuffer(ByteBuffer byteBuffer, int position) {
        return byteBuffer.getDouble(position);
    }

    @Override
    public int compareTo(Object a, Object b) {
        if(a instanceof Double && b instanceof Double) {
            return ((Double) a).compareTo((Double) b);
        }
        throw new IllegalArgumentException("Both arguments must have the same type");
    }

    public Types getTypeEnum() {
    	return Types.DOUBLE;
    }

}
